package com.tuanbiao;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hzjq.data.entity.BlastLonLat;
import com.hzjq.lib.config.param.BlastLoader;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.tencent.bugly.Bugly;
import com.tuanbiao.blast.JQBlast;
import com.tuanbiao.keyevent.KeyEventHelper;
import com.tuanbiao.other.DebugLoggerTree;
import com.tuanbiao.utils.SPHelper;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/1 15:09
 */
public class App extends Application {
    private static final String TAG = "App";
    private static App INSTANCE;
    private BlastLonLat currentLocation;

    public static App getInstance() {
        return INSTANCE;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
        SPHelper.setLoginValid(true);
        initSDK();
    }

    private void initSDK() {
        Bugly.init(getApplicationContext(), "21289785ac", !AppConfig.isDebug());
        // 初始化日志打印
        if (AppConfig.isLogEnable()) {
            Timber.plant(new DebugLoggerTree());
        }
        Timber.i(AppConfig.getBuildType());
        KeyEventHelper.getInstance().init();
        registerActivityLifecycleCallbacks(new IActivityLifecycleCallbacks());
        JQBlast.getInstance().init();
    }

    public BlastLonLat getCurrentLocation() {
        return currentLocation;
    }

    public void setCurrentLocation(BlastLonLat currentLocation) {
        this.currentLocation = currentLocation;
//        SPHelper.setCurrentLocation(currentLocation.latitude,currentLocation.longitude);
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.common_primary_color, android.R.color.white);//全局设置主题颜色
            return new MaterialHeader(context).setColorSchemeResources(R.color.common_primary_dark_color);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            return new ClassicsFooter(context).setDrawableSize(20);
        });
    }


    private static class IActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {

        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
            Timber.i("onActivityCreated#" + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {

        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {

        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {

        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {
            Timber.i("onActivityDestroyed#" + activity.getClass().getSimpleName());
        }
    }


    public static void exitRestart(Context context) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
                LaunchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(LaunchIntent);
            }
        }, 100);// 1秒钟后重启应用
    }
}
